<?php

namespace Modules\Agent;

use Modules\Agent\Entities\Brokerage;
use Modules\Agent\Entities\InviteRecord;
use Modules\Agent\Entities\InviteRecord2;
use Modules\Order\Entities\BaseOrder as Order;

class Cron
{
    public function handle($schedule)
    {
        $that = $this;
        $schedule->call(function() use ($that) {
            try {
                $that->everyMinute();
                $that->everyDay();
            }
            catch (\Throwable $e) {
                \Log::error('cron任务异常');
                \Log::error($e->getMessage());
            }
        })->everyMinute();

        $schedule->call(function() use ($that) {
            $that->everyDay();
        })->daily();
    }

    public function everyMinute()
    {
        // 已支付但未创建佣金的订单
        $orders = Order::paid()->where('brokerage_status', 0)->get();
        foreach ($orders as $order) {
            if ( $order->user_id == 14875 || $order->user_id < 500 ){
                $order->update([
                    'brokerage_status' => 1
                ]);
                continue;
            }
            Brokerage::createWith($order);
        }

        // 完成订单佣金(发放)
        $orders = Order::status('completed')->where('brokerage_status', 2)->get();
        foreach ($orders as $order) {
            foreach( $order->brokerages as $item ) {
                $item->complete();
            }
            $order->update([
                'brokerage_status' => 3
            ]);
        }

        // 取消的订单（取消佣金）
        $orders = Order::status('closed')->where('brokerage_status', 2)->get();
        foreach ($orders as $order) {
            foreach( $order->brokerages as $item ) {
                $item->close();
            }
            $order->update([
                'brokerage_status' => 4
            ]);
        }


        // 发放邀请佣金
        $inviteRecords = InviteRecord::where('status', 0)->get();
        foreach ($inviteRecords as $item) {

            // 更新邀请人数
            if ($item->direct_inviter) {
                $item->direct_inviter->updateInviteeCount();
            }

            // 更新邀请人数
            if ($item->indirect_inviter) {
                $item->indirect_inviter->updateInviteeCount();
            }

            $item->sendReward();

            if ( $item->invite_node_id == '14875' ){
                // 17660941185 不发放佣金
                continue;
            }

            // 如果此邀请记录关联某个活动、还要进行活动的奖励发放
            if ($item->invite_node_type) {
                $inviteNode = InviteRecord::getNode($item->invite_node_type, [
                    'id' => $item->invite_node_id
                ]);

                // 由具体的节点发放邀请奖励
                if ($inviteNode) {
                    $inviteIndex = $item->node_invite_index;

                    // 发放邀请奖励
                    $nodeReward = $inviteNode->sendInviteReward($item->direct_inviter, $item->user, $inviteIndex);

                    // 保存发放的奖励
                    if ($nodeReward) {
                        $item->update([
                            'node_reward' => $nodeReward
                        ]);
                    }

                    $inviteNode->update([
                        'invite_user_total' => $inviteNode->invite_user_total + 1
                    ]);
                }
            }

        }

        // 已支付但未创建分享记录的订单
        $orders = Order::paid()->where([
            'share_status' => 0,
            ['sharer', '>', 0]
        ])->get();
        foreach ($orders as $order) {
            InviteRecord2::createWith($order);
        }

        // 完成订单佣金(发放)
        $orders = Order::status('completed')->where('share_status', 2)->get();
        foreach ($orders as $order) {
            $order->update([
                'share_status' => 3
            ]);
            InviteRecord2::where('order_id', $order->id)->update([
                'status' => 0
            ]);
        }

        // 取消的订单（取消佣金）
        $orders = Order::status('closed')->where('share_status', 2)->get();
        foreach ($orders as $order) {
            $order->update([
                'share_status' => 4
            ]);
            InviteRecord2::where('order_id', $order->id)->update([
                'status' => 2
            ]);
        }


        // 发放分享佣金
        $inviteRecords = InviteRecord2::where('status', 0)->get();
        foreach ($inviteRecords as $item) {
            $item->update([
                'status' => 1
            ]);

            if ( $item->invite_node_id == '14875' ){
                // 17660941185 不发放佣金
                continue;
            }
            $total = InviteRecord2::where([
                'direct_inviter_id' => $item->direct_inviter_id,
                'invite_node_id' => $item->invite_node_id,
                ['id', '<', $item->id]
            ])->count();

            $item->update([
                'node_invite_index' => $total + 1
            ]);

            // 如果此邀请记录关联某个活动、还要进行活动的奖励发放
            if ($item->invite_node_type) {
                $inviteNode = InviteRecord2::getNode($item->invite_node_type, [
                    'id' => $item->invite_node_id
                ]);

                // 由具体的节点发放邀请奖励
                if ($inviteNode) {
                    $inviteIndex = $item->invite_index;

                    // 发放分享奖励
                    $nodeReward = $inviteNode->sendInviteRewardNew($item->direct_inviter, $item->user, $inviteIndex);

                    // 保存发放的奖励
                    if ($nodeReward) {
                        $item->update([
                            'node_reward' => $nodeReward
                        ]);
                    }

                    $inviteNode->update([
                        'share_user_total' => $inviteNode->invite_user_total + 1
                    ]);
                }
            }

        }
    }

    public function everyDay()
    {
    }
}